package jkoa.middleware;

import java.util.function.Consumer;

import jkoa.core.Context;
import jkoa.core.Middleware;
import jkoa.core.Next;

import java.util.ArrayList;
import java.util.List;

public class RouterMiddleware implements Middleware {

	private List<RoutingPair> routes = new ArrayList<>();

	public RouterMiddleware() {
	}

	public void get(String path, Middleware middleware) {
		routes.add(new RoutingPair("GET", path, middleware));
	}

	public void post(String path, Middleware middleware) {
		routes.add(new RoutingPair("POST", path, middleware));
	}

	public void use(Middleware middleware) {
		routes.add(new RoutingPair(null, null, middleware));
	}

	public void handleRequest(String method, String path, Context ctx) {
		for (RoutingPair routingPair : routes) {
			if ((routingPair.method == null || routingPair.method.equals(method))
					&& (routingPair.path == null || routingPair.path.equals(path))) {
				routingPair.middleware.handle(ctx, (ctx2) -> {
				});
				return;
			}
		}
		ctx.response.setStatusCode(404);
		ctx.response.setBody("Not Found");
	}

	private static class RoutingPair {
		String method;
		String path;
		Middleware middleware;

		RoutingPair(String method, String path, Middleware middleware) {
			this.method = method;
			this.path = path;
			this.middleware = middleware;
		}
	}

	@Override
	public void handle(Context ctx, Next next) {
		// TODO Auto-generated method stub

	}
}